package com.xlh.virtualization.controller;

import com.xlh.aop.PreAcquireLock;
import com.xlh.common.Result;
import com.xlh.security.SpringSecurityUserInfoHolder;
import com.xlh.security.UserInfoHolder;
import com.xlh.virtualization.domain.ContainerContext;
import com.xlh.virtualization.domain.ExperimentContext;
import com.xlh.virtualization.service.ContainerService;
import com.xlh.virtualization.service.ExperimentService;
import com.xlh.virtualization.vo.ExperimentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.xlh.virtualization.constant.ContextTypeEnum.QUESTION;
import static com.xlh.virtualization.constant.ExperimentConstant.*;

/**
 * @author cheer
 */
@Api("考试实验")
@RestController
@RequestMapping("/experiment/question")
public class ExperimentQuestionController extends BaseExperimentController {

    @Autowired
    public ExperimentQuestionController(UserInfoHolder userInfoHolder, ExperimentService experimentService,
                                        ContainerService containerService) {
        super.userInfoHolder = userInfoHolder;
        super.experimentService = experimentService;
        super.containerService = containerService;
    }

    @ApiOperation("考试中进入实验（获取虚拟机）/ 刷新")
    @GetMapping("/{questionId}")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    @PreAcquireLock(value = ENTER_EXPERIMENT_QUESTION_KEY, timeout = ENTER_EXPERIMENT_TIME_OUT,
            message = ENTER_EXPERIMENT_LOCK_MESSAGE)
    public Result<ExperimentVO> enterExperiment(@PathVariable("questionId") Long questionId,
                                                @Autowired SpringSecurityUserInfoHolder userInfoHolder) {
        return super.enterExperiment(questionId);
    }

    @ApiOperation("考试中初始化实验")
    @GetMapping("/{questionId}/init")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    @PreAcquireLock(value = ENTER_EXPERIMENT_QUESTION_KEY, timeout = ENTER_EXPERIMENT_TIME_OUT,
            message = ENTER_EXPERIMENT_LOCK_MESSAGE)
    public Result<ExperimentVO> initExperiment(@PathVariable("questionId") Long questionId,
                                               @Autowired SpringSecurityUserInfoHolder userInfoHolder) {
        return super.initExperiment(questionId);
    }

    @Override
    protected ContainerContext getContainerContext(Long userId, Long taskId) {
        return new ContainerContext()
                .setUserId(userId)
                .setContextType(QUESTION.getCode())
                .setContextId(taskId);
    }

    @Override
    protected ExperimentContext getExperimentContext(Long userId, Long taskId) {
        return new ExperimentContext()
                .setUserId(userId)
                .setContextType(QUESTION.getCode())
                .setContextId(taskId);
    }
}
